import org.junit.Test;
import org.lionsoul.ip2region.ipv6.Ipdbv6_buff;
import org.lionsoul.ip2region.xdb.Searcher;
import sun.net.util.IPAddressUtil;

import java.util.Arrays;

public class Demo {
    @Test
    public void testIpv4() {
        try {
            String path = "C:/Users/len/Desktop/ip2region/ip2region/data/ip2region.xdb";
            byte[] bytes = Searcher.loadContentFromFile(path);
            Searcher searcher = Searcher.newWithBuffer(bytes);
            String ips[] = {"127.0.0.1",
                    "192.123.21.145",
                    "212.64.63.215",
                    "2001:db8::ffff:1111:2222",
                    "12.12.12"
            };
            Arrays.stream(ips).forEach(ip -> {
                try {
                    if (isIpv4(ip)) {
                        out(ip, searcher.search(ip));
                    } else {
                        out(ip, "is not a ipv4");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testIpv6() {
        try {
            String path = "C:/Users/len/Desktop/ip2region/ip2region/data/ipv6wry.db";
//            Ipdbv6 db6 = new Ipdbv6(path);
            Ipdbv6_buff db6 = new Ipdbv6_buff(path);
            String ip = "2409:8a7c:6000::ffff";
            out(ip, db6.query(ip).getDisplay());
            ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
            out(ip, db6.query(ip).getDisplay());
            ip = "fe80::57cf:a558:cc36:2294";
            out(ip, db6.query(ip).getDisplay());
            ip = "2001:db8::ffff:1111:2222";
            out(ip, db6.query(ip).getDisplay());
            ip = "::1";
            out(ip, db6.query(ip).getDisplay());
            ip = "2406:840:20::11";
            out(ip, db6.query(ip).getDisplay());
            ip = "2409:891e:9342:12f9:c46a:e1f1:4667:dff2";
            out(ip, db6.query(ip).getDisplay());
            ip = "2409:892e:9342:12f9:c46a:e1f1:4667:dff2";
            out(ip, db6.query(ip).getDisplay());
            ip = "2404:6800:8005::69";
            out(ip, db6.query(ip).getDisplay());
            ip = "2409:8a00:9000::2409:8a00:9bff:11";
            out(ip, db6.query(ip).getDisplay());//中国	北京市	怀柔区 中国移动公众宽带
            ip = "2409:8a04:b00::11";
            out(ip, db6.query(ip).getDisplay());//中国	河北省	石家庄市	平山县 中国移动公众宽带
            ip = "2409:8a04:1400::11";
            out(ip, db6.query(ip).getDisplay());//中国	河北省	石家庄市	深泽县 中国移动公众宽带
            ip = "2409:8a0c:1a00::11";
            out(ip, db6.query(ip).getDisplay());//中国	山西省	大同市	新荣区 中国移动公众宽带
            ip = "2409:8a0c:da00::11";
            out(ip, db6.query(ip).getDisplay());//中国	山西省	运城市	闻喜县 中国移动公众宽带
            ip = "2409:8a10:6e00::11";
            out(ip, db6.query(ip).getDisplay());//中国	内蒙古区	通辽市	科尔沁左翼中旗 中国移动公众宽带
            ip = "2607:fcd0:da80:c00::3e8:1";
            out(ip, db6.query(ip).getDisplay());//美国	Texas州	Dallas QuadraNet Enterprises LLC
            ip = "2409:892e:9342:12f9:c46a:e1f1:4667:dff2";
            out(ip, db6.query(ip).getDisplay());//
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void out(String ip, String region) {
        System.out.println(ip + ": " + region);
    }

    private boolean isIpv4(String ip) {
        return IPAddressUtil.isIPv4LiteralAddress(ip);
    }

    private boolean isIpv6(String ip) {
        return IPAddressUtil.isIPv6LiteralAddress(ip);
    }
}
